using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ScoreAction { ADD = 0, UPDATE = 1, REMOVE = 2 }

public class ScoreEvent : CallEvent {
	private int player;
	private ScoreAction action;
	private int score;
	
	public ScoreEvent() {}
	
	public ScoreEvent(int player, ScoreAction action, int score) {
		this.player 	= player;
		this.action 	= action;
		this.score 		= score;
	}
	
	public Transform getPlayer() 	{return findPlayerByID(player);}
	public int getPlayerID() 		{return player;}
	public ScoreAction getAction() 	{return action;}
	public int getScore() 			{return score;}
	
	// Sets all the members of this ScoreEvent using a json string.
	public override void fromJSON(string json) {
		JSON obj = new JSON(json);
		player   = obj.getInt("player");
		action   = (ScoreAction) obj.getInt("action");
		score    = obj.getInt("score");
	}
	
	// Converts this event into a json string.
	public override string toJSON() {
		return new JSON()
			.setInt("player", player)
			.setInt("action", (int) action)
			.setInt("score",  score)
			.build();
	}
	
	// Finds the player associated with a particular ID.
	private Transform findPlayerByID(int id) {
		if (id == NID.unassigned) {return null;}
		else {
			NetworkView result = NetworkView.Find(IDManager.inst().findByID(id));
			if (result == null) {return null;}
			else {return result.transform;}
		}
	}
	
}